package com.shanshi.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shanshi.activity.adapter.StoreInfoAdapter;
import com.shanshi.activity.response.ResStoreInfo;
import com.shanshi.activity.tool.CustomRefreshListView;
import com.shanshi.activity.tool.OkHttpTool;

import java.util.ArrayList;
import java.util.List;

public class StoreInfo extends MainActivity implements OnClickListener{

	private SharedPreferences sharedPreferences;
	private TextView tv;
	private ImageView Iv;
	private StoreInfoAdapter storeInfoAdapter;
	private List<ResStoreInfo.Data> list;
	private List<ResStoreInfo.Data> listdata=new ArrayList<>();
	private CustomRefreshListView storeView;
	private int nowStoreId;//当前 店铺id
	private int pageIndex;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.store_info);

		//获得当前店铺的id
		sharedPreferences = StoreInfo.this.getSharedPreferences("share", MODE_PRIVATE);
		nowStoreId = sharedPreferences.getInt("nowStoreId", 0);

		tv = (TextView) findViewById(R.id.title);	
		tv.setText("切换门店");
		Iv = (ImageView) findViewById(R.id.fanhui);
		Iv.setOnClickListener(this);
		storeView = (CustomRefreshListView) findViewById(R.id.storeInfo);
		list = new ArrayList<ResStoreInfo.Data>();
		storeInfoAdapter=new StoreInfoAdapter(this, listdata,nowStoreId);
		storeView.setAdapter(storeInfoAdapter);
		getdatas();
		storeView.setOnRefreshListener(new CustomRefreshListView.OnRefreshListener() {

			@Override
			public void onPullRefresh() {
						listdata.clear();
						pageIndex = 0;
						getdatas();
			}

			@Override
			public void onLoadingMore() {
				pageIndex++;
				getdatas();
			}
		});

		// 点击门店后的事件
		storeView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					SharedPreferences.Editor editor = sharedPreferences.edit();
					editor.putInt("nowStoreId", list.get(position - 1).getStoreId());
					Log.e("===nowStoreId===", list.get(position - 1).getStoreId() + "");
					editor.commit();
					Intent intent = new Intent(StoreInfo.this, SpreadMessage.class);
					startActivity(intent);
			}
		});


	}

	
	@Override
	public void onClick(View v) {
		finish();
	}
	
	public void getdatas(){
		double longitude = Double.parseDouble(sharedPreferences.getString("userLongitude", 0+""));
		double latitude = Double.parseDouble(sharedPreferences.getString("userLatitude", 0+""));

		OkHttpTool.doGet(OkHttpTool.BASE_URL+"stores?userId=0&pageIndex="+pageIndex+"&pageSize=4&longitude="+longitude+"&latitude="+latitude
				, new OkHttpTool.setOkhttpListener() {
					@Override
					public void onSuccess(String s) {
						// TODO Auto-generated method stub
						list.clear();
						Gson gson=new Gson();
						ResStoreInfo content=gson.fromJson(s, ResStoreInfo.class);
						list.addAll(content.getData());
						Log.e("===nowStoreId====result",list.toString());
						handler.sendEmptyMessage(1);
						//storeInfoAdapter.notifyDataSetChanged();
					}

					@Override
					public void onFail() {
						// TODO Auto-generated method stub
						Log.e("=========sa","sadfa");
						handler.sendEmptyMessage(2);
					}
				});

	}


	Handler handler=new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case 1:
					listdata.addAll(list);
					Log.e("store", "handleMessage: "+pageIndex );
					storeInfoAdapter.notifyDataSetChanged();
					storeView.completeRefresh();
					break;
				case 2:
					Toast.makeText(StoreInfo.this,"刷新失败",Toast.LENGTH_SHORT).show();
					storeView.completeRefresh();
			}
		}
	};


}
